{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# watsonx X BeeAI \n",
    "\n",
    "This notebook showcases a series of examples that demonstrate how to integrate BeeAI with watsonx.\n",
    "\n",
    "To run these examples, you'll need a set of watsonx credentials:\n",
    "\n",
    "- WATSONX_API_KEY\n",
    "- WATSONX_PROJECT_ID\n",
    "- WATSONX_API_URL\n",
    "\n",
    "Please enter your credentials in the next cell before proceeding."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "WATSONX_API_KEY = \"\"\n",
    "WATSONX_PROJECT_ID = \"\"\n",
    "WATSONX_API_URL = \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## WatsonX ChatModel\n",
    "\n",
    "This example demonstrates how to create a ChatModel to interface with the ibm/granite-3-8b-instruct model from watsonx."
   ]
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "from beeai_framework.backend import ChatModel, ChatModelOutput, UserMessage\n",
    "\n",
    "# Create a ChatModel to interface with ibm/granite-3-8b-instruct from watsonx\n",
    "model = ChatModel.from_name(\n",
    "    \"watsonx:ibm/granite-3-8b-instruct\",\n",
    "    options={\n",
    "        \"project_id\": WATSONX_PROJECT_ID,\n",
    "        \"api_key\": WATSONX_API_KEY,\n",
    "        \"api_base\": WATSONX_API_URL,\n",
    "    },\n",
    ")\n",
    "\n",
    "message = UserMessage(content=\"Briefly explain quantum computing in simple terms with an example.\")\n",
    "output: ChatModelOutput = await model.run([message])\n",
    "\n",
    "print(output.get_text_content())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Structured Outputs With watsonx\n",
    "\n",
    "This example demonstrates how to generate structured output using the ibm/granite-3-8b-instruct model from watsonx."
   ]
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "import json\n",
    "\n",
    "from pydantic import BaseModel, Field\n",
    "\n",
    "\n",
    "# The output structure definition, note the field descriptions that can help the LLM to understand the intention of the field.\n",
    "class BookDetailsSchema(BaseModel):\n",
    "    title: str = Field(description=\"The title of the book.\")\n",
    "    author: str = Field(description=\"The author of the book.\")\n",
    "    plot_summary: str = Field(description=\"A brief summary of the plot.\")\n",
    "\n",
    "\n",
    "user_message = UserMessage(\"Provide a summary of the following book: 'Dune' by Frank Herbert.\")\n",
    "\n",
    "response = await model.run([user_message], response_format=BookDetailsSchema)\n",
    "\n",
    "print(json.dumps(response.output_structured, indent=4))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
